package icraf.carbon.gui.chart;

import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_AXIS_MAIN;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_AXIS_UNIT;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_BACKGROUND;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_BAR;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_LABEL;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_LEGEND;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_THRESHOLD;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_X_AXIS;
import static icraf.carbon.gui.MenuFactory.MenuProperty.CHART_FORMAT_Y_AXIS;
import icraf.carbon.gui.MenuFactory.MenuProperty;
import icraf.common.gui.MenuAdapter;
import icraf.common.gui.MenuGenerator.IconModel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import com.l2fprod.common.swing.JButtonBar;
import com.l2fprod.common.swing.plaf.blue.BlueishButtonBarUI;

public class ChartSettingPanel extends JPanel {
	private static final long serialVersionUID = 1382010458109095332L;

	public static void main(String[] args) {
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Windows".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {

		}

		JPanel p = new ChartSettingPanel(null);

		final JFrame frame = new JFrame();
		frame.setSize(new Dimension(450, 450));
		frame.getContentPane().add(p, BorderLayout.CENTER);

		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}

		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frame.setVisible(true);
			}
		});

	}
	
	
	private AbacusPanel abacusPanel;
	private Component currentComponent;
//	private MenuAdapter[] menuToolBar = { CHART_FORMAT_X_AXIS,
//			CHART_FORMAT_Y_AXIS, CHART_FORMAT_BAR, CHART_FORMAT_LABEL,
//			CHART_FORMAT_THRESHOLD, CHART_FORMAT_LEGEND,
//			CHART_FORMAT_BACKGROUND };
	
	private LineLabelSettingPanel xAxisPanel = new LineLabelSettingPanel(CHART_AXIS_MAIN, CHART_AXIS_UNIT);
	private LineLabelSettingPanel yAxisPanel = new LineLabelSettingPanel(CHART_AXIS_MAIN, CHART_AXIS_UNIT);
	private LineLabelSettingPanel thresholdPanel = new LineLabelSettingPanel(MenuProperty.CHART_THRESHOLD_LINE_COST, MenuProperty.CHART_THRESHOLD_LINE_EMISSION);
	private ChartFormat chartSetting = new ChartFormat();
	private LabelSettingPanel labelPanel = new LabelSettingPanel();
	private FontChooser legendPanel = new FontChooser();
	private ShapeSettingPanel backgroundPanel = new ShapeSettingPanel(ShapeSettingPanel.COLOR_SETTING);
	private BarSettingPanel barPanel = new BarSettingPanel();
	
	public ChartSettingPanel(AbacusPanel abacusPanel) {
		this.abacusPanel = abacusPanel;
		JButtonBar toolbar = new JButtonBar(JButtonBar.VERTICAL);
		toolbar.setUI(new BlueishButtonBarUI());
		
		
		setLayout(new BorderLayout());
		add("West", toolbar);

		
		
		ButtonGroup group = new ButtonGroup();
		
		
		MenuAdapter m = CHART_FORMAT_X_AXIS;
		JPanel p = makePanel(m.getLabel(), xAxisPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		m = CHART_FORMAT_Y_AXIS;
		p = makePanel(m.getLabel(), yAxisPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		m = CHART_FORMAT_BAR;
		p = makePanel(m.getLabel(), barPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		m = CHART_FORMAT_LABEL;
		p = makePanel(m.getLabel(), labelPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		m = CHART_FORMAT_LEGEND;
		p = makePanel(m.getLabel(), legendPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		m = CHART_FORMAT_THRESHOLD;
		p = makePanel(m.getLabel(), thresholdPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		m = CHART_FORMAT_BACKGROUND;
		p = makePanel(m.getLabel(), backgroundPanel);
		addButton(m.getLabel(), m.getIcon(IconModel.ICON32), p, toolbar, group);
		
		xAxisPanel.setSettingA(chartSetting.canvas.xAxis);
		xAxisPanel.setSettingB(chartSetting.canvas.xUnit);
		yAxisPanel.setSettingA(chartSetting.canvas.yAxis);
		yAxisPanel.setSettingB(chartSetting.canvas.yUnit);
	}

	private void addButton(String title, ImageIcon icon,
			final Component component, JButtonBar bar, ButtonGroup group) {
		Action action = new AbstractAction(title, icon) {
			private static final long serialVersionUID = 5073139705808499427L;
			public void actionPerformed(ActionEvent e) {
				show(component);
			}
		};

		JToggleButton button = new JToggleButton(action);
		bar.add(button);
		
		group.add(button);
		if (group.getSelection() == null) {
			button.setSelected(true);
			show(component);
		}
	}

	AbacusPanel getAbacusPanel() {
		return abacusPanel;
	}

	private JPanel makePanel(String title, Component content) {
		JPanel panel = new JPanel(new BorderLayout(4,4));
		JLabel top = new JLabel(title);
		top.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		top.setFont(top.getFont().deriveFont(Font.BOLD));
		top.setOpaque(true);
		top.setBackground(panel.getBackground().brighter());
		panel.add("North", top);
		panel.setPreferredSize(new Dimension(400, 300));
		panel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
		panel.add(content);
		return panel;
	}

	private void show(Component component) {
		if (currentComponent != null) {
			remove(currentComponent);
		}
		add("Center", currentComponent = component);
		revalidate();
		repaint();
	}

	public ChartFormat getChartSetting() {
		chartSetting.canvas.xAxis = xAxisPanel.getSettingA(chartSetting.canvas.xAxis);
		chartSetting.canvas.xUnit = xAxisPanel.getSettingB(chartSetting.canvas.xUnit);
		chartSetting.canvas.yAxis = yAxisPanel.getSettingA(chartSetting.canvas.yAxis);
		chartSetting.canvas.yUnit = yAxisPanel.getSettingB(chartSetting.canvas.yUnit);
		
		chartSetting.barLegend.clear();
		chartSetting.barLegend.addAll(barPanel.getShapeSettingList());
		
		chartSetting.label = labelPanel.getLabelSetting(chartSetting.label);
		chartSetting.legend = legendPanel.getFontFormat();
		
		chartSetting.thresholdCost = thresholdPanel.getSettingA(chartSetting.thresholdCost);
		chartSetting.thresholdEmission = thresholdPanel.getSettingB(chartSetting.thresholdEmission);
		
		chartSetting.canvas.background = backgroundPanel.getSetting(chartSetting.canvas.background);
		return chartSetting;
	}

	public BarSettingPanel getBarPanel() {
		return barPanel;
	}

	public void setChartFormat(ChartFormat chartSetting) {
		this.chartSetting = chartSetting;
		
		xAxisPanel.setSettingA(chartSetting.canvas.xAxis);
		xAxisPanel.setSettingB(chartSetting.canvas.xUnit);
		yAxisPanel.setSettingA(chartSetting.canvas.yAxis);
		yAxisPanel.setSettingB(chartSetting.canvas.yUnit);
		
		barPanel.setShapeSettingList(chartSetting.barLegend);
		
		labelPanel.setLabelSetting(chartSetting.label);
		legendPanel.setFontFormat(chartSetting.legend);
		thresholdPanel.setSettingA(chartSetting.thresholdCost);
		thresholdPanel.setSettingB(chartSetting.thresholdEmission);
		
		 backgroundPanel.setSetting(chartSetting.canvas.background);
	}

}